package com.easydisk.file.controller;

import com.easydisk.file.model.req.RecycleBinDeleteReq;
import com.easydisk.file.model.req.RestoreReq;
import com.easydisk.file.model.vo.UserFileVO;
import com.easydisk.file.service.RecycleService;
import com.easydisk.response.ApiResponse;
import com.easydisk.common.util.SecurityContextUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/file")
public class RecycleController {

    @Autowired
    private RecycleService recycleService;

    /**
     * 获取回收站的文件列表
     *
     */
    @ApiOperation(
            value = "获取回收站的文件列表",
            notes = "该接口提供了获取回收站的文件列表的功能",
            consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE
    )
    @GetMapping("recycles")
    public ApiResponse<List<UserFileVO>> getRecycleList() {
        Long userId = SecurityContextUtil.getUserId();
        return ApiResponse.data(recycleService.getRecycleList(userId));
    }

    /**
     * 还原文件
     *
     * @return
     */
    @ApiOperation(
            value = "还原文件",
            notes = "该接口提供了还原文件的功能",
            consumes = MediaType.APPLICATION_JSON_UTF8_VALUE,
            produces = MediaType.APPLICATION_JSON_UTF8_VALUE
    )
    @PutMapping("recycle/restore")
    public ApiResponse restore(@Validated @RequestBody RestoreReq restoreReq) {
        Long userId = SecurityContextUtil.getUserId();

        recycleService.restore(restoreReq.getFileIds(), userId);
        return ApiResponse.success();
    }

    /**
     * 回收站删除文件
     *
     * @return
     */
    @ApiOperation(
            value = "回收站删除文件",
            notes = "该接口提供了回收站删除文件的功能"
    )
    @DeleteMapping("recycle")
    public ApiResponse delete(@Validated @RequestBody RecycleBinDeleteReq recycleBinDeleteReq) {
        Long userId = SecurityContextUtil.getUserId();
        recycleService.delete(recycleBinDeleteReq.getFileIds(),userId);
        return ApiResponse.success();
    }
}
